package editorview.views;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchPartReference;
import org.eclipse.ui.IWorkbenchPartSite;

import editorview.views.actions.CloseAction;

public class EditorsTreeViewer {

  private TreeViewer viewer;

  public EditorsTreeViewer(Composite parent, int style, IWorkbenchPartSite site) {
    viewer = new TreeViewer(parent, style);
    viewer.setContentProvider(new EditorsViewTreeContentProvider(viewer));
    viewer.getTree().setHeaderVisible(true);
    TreeViewerColumn columnFileName = new TreeViewerColumn(viewer, SWT.LEFT);
    columnFileName.setLabelProvider(new FileNameLabelProvider());
    columnFileName.getColumn().setText("Name");
    columnFileName.getColumn().setWidth(120);
    TreeViewerColumn columnPath = new TreeViewerColumn(viewer, SWT.LEFT);
    columnPath.setLabelProvider(new FilePathProvider());
    columnPath.getColumn().setText("Path");
    columnPath.getColumn().setWidth(100);
    // viewer.setSorter(new NameSorter());
    // viewer.setInput(getViewSite());
    viewer.addDoubleClickListener(new IDoubleClickListener() {

      @Override
      public void doubleClick(DoubleClickEvent event) {
        ISelection selection = event.getSelection();
        if (selection instanceof TreeSelection) {
          TreeSelection treeSelection = (TreeSelection) selection;
          Object firstElement = treeSelection.getFirstElement();
          if (firstElement instanceof IEditorReference) {
            IEditorReference editorReference = (IEditorReference) firstElement;
            editorReference.getPage().bringToTop(editorReference.getPart(true));
          }
        }
        selection.isEmpty();
      }
    });
    setInput(site.getPage());
    site.getPage().addPartListener(new EditorViewerPartListerner(this));
    hookContextMenu(site);
  }

  public void setFocus() {
    viewer.getControl().setFocus();
  }

  public void setInput(Object input) {
    if (!viewer.getTree().isDisposed()) {
    viewer.setInput(input);
    }
  }

  public void addEditor(IWorkbenchPartReference partRef) {
  }

  private void hookContextMenu(final IWorkbenchPartSite site) {
    MenuManager menuMgr = new MenuManager("#PopupMenu");
    menuMgr.setRemoveAllWhenShown(true);
    menuMgr.addMenuListener(new IMenuListener() {

      public void menuAboutToShow(IMenuManager manager) {
        fillContextMenu(manager);
      }
    });
    Menu menu = menuMgr.createContextMenu(viewer.getControl());
    viewer.getControl().setMenu(menu);
    site.registerContextMenu(menuMgr, viewer);
  }

  private void fillContextMenu(IMenuManager manager) {
    IAction closeAction = new CloseAction(this);
    manager.add(closeAction);
    //    manager.add(action2);
    // Other plug-ins can contribute there actions here
    manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
  }

  public IEditorReference[] getSelectedEditors() {
    List<IEditorReference> selectedEditors = new ArrayList<IEditorReference>();
    ISelection selection = viewer.getSelection();
    if (selection instanceof TreeSelection) {
      TreeSelection treeSelection = (TreeSelection) selection;
      Iterator<?> iterator = treeSelection.iterator();
      while (iterator.hasNext()) {
        Object object = (Object) iterator.next();
        if (object instanceof IEditorReference) {
          selectedEditors.add((IEditorReference) object);
        }
      }

    }
    return selectedEditors.toArray(new IEditorReference[0]);
  }

}
